var Fenzuguanli = {
	param : function (){
//		var group_id = "1653817287861";
		var group_id = getQueryString("group_id");
		return function () {
			return group_id;
		}
	},
	init : function () {
		$(".add-city").find("input[name=save]").click(Fenzuguanli.add_teacher);
	},
	teacher_list : function (result) {
		if (result != null && result.data != null && result.data.teachers.length >= 0) {
			var dd = $(".add-mygroup-dl").find("dd[class=cur]");
			$(".mygroup-header").html($(dd).text() + '<span class="num">组员人数：' + result.data.teachers.length + '人</span><a href="fenzuguanli-xinzengzuyuan.html" class="btn-5faee3">新增组员</a><input class="btn-ccc" type="button" name="remove_teacher" value="删除组员">' + 
				'<input class="btn-5faee3 dialogShow" type="button" name="bianji" value="编辑组名"><input class="btn-5faee3" type="button" name="jiesan" value="解散分组">');
    			var content = $(".table-list tbody");
    			content.empty();
    			var teachers = result.data.teachers;
    			var trs = "";
    			for (var i = 0; i < teachers.length; i++) {
    				var userOne = teachers[i];
    				trs += ("<tr>");
    				var id = ("<td><input type='checkbox' name='teacher_id' value='" + userOne.id +"' /></td>");
    				var img = ('<td><img src="../images/man.jpg" width="36" height="36"/></td>');
    				var nick = ("<td>" + (userOne.nick == undefined ? "" : userOne.nick) +"</td>");
    				var genderDesc = "未知";
    				if (userOne.gender == 0) {
    					genderDesc = "未知";
    				} else if (userOne.gender == 1) {
    					genderDesc = "女性";
    				} else if (userOne.gender == 2) {
    					genderDesc = "男性";
    				}
    				var gender = ("<td>" + genderDesc +"</td>");
    				var birthday = ("<td>" + userOne.birthday +"</td>");
    				var mobile = ("<td>" + userOne.mobile +"</td>");
    				var statusDesc = "未知";
    				if (userOne.id_status == 0) {
    					statusDesc = "未认证";
    				} else if (userOne.type == 1) {
    					statusDesc = "已认证";
    				} else if (userOne.type == 2) {
    					statusDesc = "认证失败";
    				}
    				var status = ("<td>" + statusDesc +"</td>");
    				var operate = ('<td><span class="btn-border dialogShow">教练详情</span></td>');
    				trs += ( id + img + nick + gender + birthday + mobile + status + operate);
    				trs += ("</tr>");
    			}
    			content.append(trs);
    		}
	},
	add_teacher : function () {
		var ids = $(".table-list tbody").find("input[name=teacher_id]:checked");
		if (ids== null || ids.length == 0) {
			return;
		}
		var user_ids = [];
		for( var i=0; i<ids.length; i++) {
			user_ids.push($(ids[i]).val());
		}
		var group_id = $(".add-mygroup-dl").find("dd[class=cur]").attr("group_id");
		var data = {
			"group_id" : Fenzuguanli.param()(),
			"user_ids" : user_ids
		};
		ajax("/teacher-groups/teachers/add" , data, callback_teacher_add_success);
	},
}

function getQueryString(name) {
     var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
     var r = window.location.search.substr(1).match(reg);
     if(r!=null)return  unescape(r[2]); return null;
}

function callback_teacher_list(result) {
	Fenzuguanli.teacher_list(result);
}
function callback_teacher_add_success(result) {
	window.location.href = "fenzuguanli.html";
}

function success(result) {
	if (result != null && result.code == 0) {
		$("#dialog-fenzuguanli").css({"display" : "none"});
		ajax("/teacher-groups/list", {}, callback);
	} else {
		alert("操作失败，请刷新页面重试！");
	}
}

$(function(){
	Fenzuguanli.init();
});
